﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[RequireComponent(typeof(Animator))]
public class TheWorld : MonoBehaviour
{
    [SerializeField]
    private GameObject theWorldGo;
    [SerializeField]
    private float scaleSpeed = 20;
    [SerializeField]
    private float area = 100f;

    private Animator anim;

    private void Awake()
    {
        anim = GetComponent<Animator>();
    }

    private void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            StartCoroutine(TheWorldOpen());
        }

        if (Input.GetMouseButtonDown(1))
        {
            StartCoroutine(TheWorldClose());
        }
    }

    IEnumerator TheWorldOpen()
    {
        anim.SetBool("TheWorld", true);
        while (theWorldGo.transform.localScale.x < area)
        {
            theWorldGo.transform.localScale += new Vector3(Time.deltaTime * scaleSpeed, Time.deltaTime * scaleSpeed, Time.deltaTime * scaleSpeed);
            yield return new WaitForEndOfFrame();
            if(anim.GetBool("TheWorld") == false)
            {
                break;
            }
        }
    }

    IEnumerator TheWorldClose()
    {
        anim.SetBool("TheWorld", false);
        while (theWorldGo.transform.localScale.x > 0)
        {
            theWorldGo.transform.localScale -= Vector3.one * Time.deltaTime * scaleSpeed;
            yield return new WaitForEndOfFrame();

            if (theWorldGo.transform.localScale.x < 0)
                theWorldGo.transform.localScale = Vector3.zero;
        }
    }
}
